Ext.onReady(function(){
	
	var recModule = Ext.data.Record.create(['moduleCode', 'moduleTitleKey', 'moduleTitleKeyCn', 'moduleTitleKeyEn', 'moduleDescKey','clientType']);
	var recMenuGroup = Ext.data.Record.create(['menuGrpSeq', 'menuGrpTitleKey', 'menuGrpTitleKeyCn', 'menuGrpTitleKeyEn', 'menuGrpDescKey']);
	var recMenuItem = Ext.data.Record.create(['viewId', 'menuItemUrl', 'menuItemSeq', 'menuItemDescKey','showInConsole','showInMenu','isDialog','isNewFun']);
	var recViewButton = Ext.data.Record.create(['buttonUrl', 'btnName', 'btnTitleKey', 'btnMsgKey']);
	
	var root = new Ext.tree.TreeNode({id:'root',text: system.RoleUser.functionTree});
	
	AuthFunctionService.loadAllModule(function(modules){
		// load module
	    Ext.each(modules, function(md){
			var mdNode = new Ext.tree.TreeNode({
	    		text : Gfa.CommonUtil.matchLanguage(md['moduleTitleKey']),
	    		iconCls : 'module'
			});
	    	mdNode.authType = 'Module';
	    	mdNode.authData = md;
	    	root.appendChild(mdNode);
			
			// load menuGroup
			Ext.each(md.menuGroups, function(mg){
				var mgNode = new Ext.tree.TreeNode({
		    		text : Gfa.CommonUtil.matchLanguage(mg['menuGrpTitleKey']),
		    		iconCls : 'menuGroup'
				});
		    	mgNode.authType = 'MenuGroup';
	    		mgNode.authData = mg;
	    		mdNode.appendChild(mgNode);
		    	
		    	// load menuItem
		    	Ext.each(mg.menuItems, function(mi){
		    		var miNode = new Ext.tree.TreeNode({
			    		text : Gfa.CommonUtil.matchLanguage(mi['menuItemTitleKey']),
			    		iconCls : 'menuItem'
					});
			    	miNode.authType = 'MenuItem';
		    		miNode.authData = mi;
		    		mgNode.appendChild(miNode);
		    		
			    	// load viewButton
			    	Ext.each(mi.viewButtons, function(vb){
			    		var vbNode = new Ext.tree.TreeNode({
				    		text : Gfa.CommonUtil.matchLanguage(vb['btnTitleKey']),
				    		iconCls : 'viewButton'
						});
				    	vbNode.authType = 'ViewButton';
			    		vbNode.authData = vb;
			    		miNode.appendChild(vbNode);
			    	});
		    	});
			});
	    });
	});
	
	var authTree = new Ext.tree.TreePanel({
		region : 'west',
		title: system.RoleUser.functionTree,
		border: true,
		width: 200,
		margins : '5 5 5 5',
		animate: false,
		autoScroll : true,
		rootVisible: false,
		root: root
	});
	
	var moduleId;
	var menuGroupId;
	var menuItemId;
	authTree.on('click', function(node, e){
		if (node.hasChildNodes()) node.expand();
		changeState(node.authType);
		switch(node.authType) {
			case 'Module': 
				formModule.editingRec = new recModule(node.authData);
				formModule.form.loadRecord(formModule.editingRec);
				moduleId = formModule.editingRec.data.moduleId;
				dsModuleTitle.load();
				break;
			case 'MenuGroup':
				formMenuGroup.editingRec = new recMenuGroup(node.authData);
				formMenuGroup.form.loadRecord(formMenuGroup.editingRec);
				menuGroupId = formMenuGroup.editingRec.data.menuGroupId;
				dsMenuGroupTitle.load();
				break;
			case 'MenuItem':
				formMenuItem.editingRec = new recMenuItem(node.authData);
				formMenuItem.form.loadRecord(formMenuItem.editingRec);
				menuItemId = formMenuItem.editingRec.data.menuItemId;
				dsMenuItemTitle.load();
				break;
			case 'ViewButton':
				formViewButton.editingRec = new recViewButton(node.authData);
				formViewButton.form.loadRecord(formViewButton.editingRec);
				break;
			defaults: return;
		}
	});
	
	// *** Title 公用 Begin ***
	function changeLangValueToDisplay(value, metadata, record){
		switch(record.data.lang){			
			case "en" : return "英语" ;
			case "zh_CN" : return "简体中文" ;
			case "default" : return "默认";
			default : return ""
		}
		return "";
	}
	var TitleRecord = Ext.data.Record.create([
        {name: 'lang', mapping: 0},
        {name: 'title', mapping: 1}
    ]);
	
	var storeLang = new Ext.data.SimpleStore( {
		fields : [ 'lang', 'value' ],
		data : [ [ '默认', 'default' ], [ '英语', 'en' ],[ '简体中文', 'zh_CN' ]],
		autoLoad : true
	});
	
	var comboxLang4Module = new Ext.form.ComboBox({
		editable: true,
		mode: 'local', 
		triggerAction: 'all',
		store: storeLang,
		valueField: 'value',  
		displayField: 'lang',
		hiddenName : 'value',
		name: 'lang'
	});
	
	var comboxLang4MenuGroup = new Ext.form.ComboBox({
		editable: true,
		mode: 'local', 
		triggerAction: 'all',
		store: storeLang,
		valueField: 'value',  
		displayField: 'lang',
		hiddenName : 'value',
		name: 'lang'
	});
	
	var comboxLang4MenuItem = new Ext.form.ComboBox({
		editable: true,
		mode: 'local', 
		triggerAction: 'all',
		store: storeLang,
		valueField: 'value',  
		displayField: 'lang',
		hiddenName : 'value',
		name: 'lang'
	});
	// *** Title 公用  End ***
	
	// *** ModuleTitle Begin ***
	var dsModuleTitle = new Ext.data.Store({
		proxy : new Gfa.data.DwrProxy(CommonService),
		reader : new Gfa.data.DwrReader({}, Ext.data.Record.create(['lang', 'title']))
	});
	
	dsModuleTitle.on('beforeLoad', function(store, options) {
		if (options.params === undefined) {
			options.params = {};
		}
		var queryInfo = {};
		queryInfo.queryObjectName = 'com.wef.system.entity.ModuleTitle';
		queryInfo.fields = [];
		queryInfo.listType = 'list';

		var field = {};
		field.fieldName = 'moduleId';
		field.fieldValueString = moduleId;

		queryInfo.fields.push(field);
		
		store.proxy.DWR = CommonService;
		options.params.dwrMethod = "findByQueryInit";
		options.params.dwrArguments = [queryInfo, []];
	});
	
	var gridModuleTitle = new Ext.grid.EditorGridPanel( {
				ds : dsModuleTitle,
				height : 150,
				width : 400,
				frame : true,
				clicksToEdit : 1,
				selModel : new Ext.grid.RowSelectionModel( {
					singleSelect : false
				}),
				columns : [ {
					header : system.MenuItemTitle.lang,
					width : 100,
					dataIndex : 'lang',
					editor : comboxLang4Module,
					renderer : changeLangValueToDisplay
				}, {
					header : system.MenuItemTitle.title,
					width : 280,
					dataIndex : 'title',
					editor : new Ext.form.TextField( {
						allowBlank : true
					})
				} ],
				bbar : new Ext.Toolbar( {
					items : [ {
						id : 'add',
						text : i18n.global.add,
						handler : addModuleTitle
					}, {
						id : 'delete',
						text : i18n.global.del,
						enable : false,
						handler : deleteModuleTitle
					} ]
				})
			});
	
	function addModuleTitle() {
		var n = gridModuleTitle.getStore().getCount();
		var p = new TitleRecord( {
			lang : 'default',
			title : ''
		});
		gridModuleTitle.stopEditing();
		gridModuleTitle.getStore().insert(n, p);
	}
	
	function deleteModuleTitle() {
		var sm = gridModuleTitle.getSelectionModel();
		var sel = sm.getSelected();
		if (sm.hasSelection()) {
			Ext.Msg.show( {
				title : i18n.message.msg,
				buttons : Ext.MessageBox.YESNO,
				msg : i18n.message.ifDelete,
				fn : function(btn) {
					if (btn == 'yes') {
						gridModuleTitle.getStore().remove(sel);
					}
				}
			});
		} else {
			Ext.MessageBox.alert(i18n.message.msg, '请选择一条信息，进行删除!');
		}
	}
	// *** ModuleTitle End ***
	
	// *** MenuGroupTitle Begin ***
	var dsMenuGroupTitle = new Ext.data.Store({
		proxy : new Gfa.data.DwrProxy(CommonService),
		reader : new Gfa.data.DwrReader({}, Ext.data.Record.create(['lang', 'title']))
	});
	
	dsMenuGroupTitle.on('beforeLoad', function(store, options) {
		if (options.params === undefined) {
			options.params = {};
		}
		var queryInfo = {};
		queryInfo.queryObjectName = 'com.wef.system.entity.MenuGroupTitle';
		queryInfo.fields = [];
		queryInfo.listType = 'list';

		var field = {};
		field.fieldName = 'menuGroupId';
		field.fieldValueString = menuGroupId;

		queryInfo.fields.push(field);
		
		store.proxy.DWR = CommonService;
		options.params.dwrMethod = "findByQueryInit";
		options.params.dwrArguments = [queryInfo, []];
	});
	
	var gridMenuGroupTitle = new Ext.grid.EditorGridPanel( {
				ds : dsMenuGroupTitle,
				height : 150,
				width : 400,
				frame : true,
				clicksToEdit : 1,
				selModel : new Ext.grid.RowSelectionModel( {
					singleSelect : false
				}),
				columns : [ {
					header : system.MenuItemTitle.lang,
					width : 100,
					dataIndex : 'lang',
					editor : comboxLang4MenuGroup,
					renderer : changeLangValueToDisplay
				}, {
					header : system.MenuItemTitle.title,
					width : 280,
					dataIndex : 'title',
					editor : new Ext.form.TextField( {
						allowBlank : true
					})
				} ],
				bbar : new Ext.Toolbar( {
					items : [ {
						id : 'add',
						text : i18n.global.add,
						handler : addMenuGroupTitle
					}, {
						id : 'delete',
						text : i18n.global.del,
						enable : false,
						handler : deleteMenuGroupTitle
					} ]
				})
			});
	
	function addMenuGroupTitle() {
		var n = gridMenuGroupTitle.getStore().getCount();
		var p = new TitleRecord( {
			lang : 'default',
			title : ''
		});
		gridMenuGroupTitle.stopEditing();
		gridMenuGroupTitle.getStore().insert(n, p);
	}
	
	function deleteMenuGroupTitle() {
		var sm = gridMenuGroupTitle.getSelectionModel();
		var sel = sm.getSelected();
		if (sm.hasSelection()) {
			Ext.Msg.show( {
				title : i18n.message.msg,
				buttons : Ext.MessageBox.YESNO,
				msg : i18n.message.ifDelete,
				fn : function(btn) {
					if (btn == 'yes') {
						gridMenuGroupTitle.getStore().remove(sel);
					}
				}
			});
		} else {
			Ext.MessageBox.alert(i18n.message.msg, '请选择一条信息，进行删除!');
		}
	}
	// *** MenuGroupTitle End ***
	
	// *** MenuItemTitle Begin ***
	var dsMenuItemTitle = new Ext.data.Store({
		proxy : new Gfa.data.DwrProxy(CommonService),
		reader : new Gfa.data.DwrReader({}, Ext.data.Record.create(['lang', 'title']))
	});
	
	dsMenuItemTitle.on('beforeLoad', function(store, options) {
		if (options.params === undefined) {
			options.params = {};
		}
		var queryInfo = {};
		queryInfo.queryObjectName = 'com.wef.system.entity.MenuItemTitle';
		queryInfo.fields = [];
		queryInfo.listType = 'list';

		var field = {};
		field.fieldName = 'menuItemId';
		field.fieldValueString = menuItemId;

		queryInfo.fields.push(field);
		
		store.proxy.DWR = CommonService;
		options.params.dwrMethod = "findByQueryInit";
		options.params.dwrArguments = [queryInfo, []];
	});
	
	var gridMenuItemTitle = new Ext.grid.EditorGridPanel( {
				ds : dsMenuItemTitle,
				height : 150,
				width : 400,
				frame : true,
				clicksToEdit : 1,
				selModel : new Ext.grid.RowSelectionModel( {
					singleSelect : false
				}),
				columns : [ {
					header : system.MenuItemTitle.lang,
					width : 100,
					dataIndex : 'lang',
					editor : comboxLang4MenuItem,
					renderer : changeLangValueToDisplay
				}, {
					header : system.MenuItemTitle.title,
					width : 280,
					dataIndex : 'title',
					editor : new Ext.form.TextField( {
						allowBlank : true
					})
				} ],
				bbar : new Ext.Toolbar( {
					items : [ {
						id : 'add',
						text : i18n.global.add,
						handler : addMenuItemTitle
					}, {
						id : 'delete',
						text : i18n.global.del,
						enable : false,
						handler : deleteMenuItemTitle
					} ]
				})
			});
	
	function addMenuItemTitle() {
		var n = gridMenuItemTitle.getStore().getCount();
		var p = new TitleRecord( {
			lang : 'default',
			title : ''
		});
		gridMenuItemTitle.stopEditing();
		gridMenuItemTitle.getStore().insert(n, p);
	}
	
	function deleteMenuItemTitle() {
		var sm = gridMenuItemTitle.getSelectionModel();
		var sel = sm.getSelected();
		if (sm.hasSelection()) {
			Ext.Msg.show( {
				title : i18n.message.msg,
				buttons : Ext.MessageBox.YESNO,
				msg : i18n.message.ifDelete,
				fn : function(btn) {
					if (btn == 'yes') {
						gridMenuItemTitle.getStore().remove(sel);
					}
				}
			});
		} else {
			Ext.MessageBox.alert(i18n.message.msg, '请选择一条信息，进行删除!');
		}
	}
	// *** MenuItemTitle End ***
	
	var formModule = new Ext.form.FormPanel({
		iconCls : 'module',
		title: system.Module.Module,
		disabled: true,
		labelWidth:60,
		buttonAlign: 'left',
		defaults: {width: 300},
		defaultType: 'textfield',
		tbar: gridModuleTitle,
		items:[{
			name: 'moduleCode',
			allowBlank: false,
			fieldLabel: system.Module.moduleCode
		}, {
			xtype : 'numberfield',
			name: 'clientType',
			allowBlank: false,
			fieldLabel: 'SEQ'
		}, {
			name: 'moduleDescKey',
			allowBlank: false,
			fieldLabel: system.Module.moduleDescKey
		}],
		buttons: [{
			text: i18n.global.save,
			handler: function(){ save(formModule, 'Module');}
		}, {
			text: i18n.global.del,
			handler: function(){
				remove(formModule, AuthFunctionService.saveOrUpdateModule);
			}
		}]
	});

	var formMenuGroup = new Ext.form.FormPanel({
		iconCls : 'menuGroup',
		title: system.MenuGroup.MenuGroup,
		disabled: true,
		labelWidth:90,
		buttonAlign: 'left',
		defaults: {width: 300},
		defaultType: 'textfield',
		tbar: gridMenuGroupTitle,
		items:[{
			xtype : 'numberfield',
			name: 'menuGrpSeq',
			allowBlank: false,
			fieldLabel: system.MenuGroup.menuGrpSeq
		}, {
			name: 'menuGrpDescKey',
			allowBlank: false,
			fieldLabel: system.MenuGroup.menuGrpDescKey
		}],
		buttons: [{
			text: i18n.global.save,
			handler: function(){ save(formMenuGroup, 'MenuGroup');}
		}, {
			text: i18n.global.del,
			handler: function(){
				remove(formMenuGroup, AuthFunctionService.saveOrUpdateMenuGroup);
			}
		}]
	});
	
	var formMenuItem = new Ext.form.FormPanel({
		iconCls : 'menuItem',
		title: system.MenuItem.MenuItem,
		disabled: true,
		labelWidth:100,
		buttonAlign: 'left',
		defaults: {width: 300},
		defaultType: 'textfield',
		tbar: gridMenuItemTitle,
		items:[{
			name: 'viewId',
			allowBlank: false,
			fieldLabel: system.MenuItem.viewId
		}, {
			name: 'menuItemUrl',
			fieldLabel: system.MenuItem.menuItemUrl
		}, {
			xtype : 'numberfield',
			name: 'menuItemSeq',
			allowBlank: false,
			fieldLabel: system.MenuItem.menuItemSeq
		}, {
			name: 'menuItemDescKey',
			allowBlank: false,
			fieldLabel: system.MenuItem.menuItemDescKey
		},{
			name : 'showInMenu',
			fieldLabel : system.MenuItem.showInMenu,
			xtype : 'checkbox'
		},{
			name : 'showInConsole',
			fieldLabel : system.MenuItem.showInConsole,
			xtype : 'checkbox'
		},{
			name : 'isDialog',
			fieldLabel : system.MenuItem.isDialog,
			xtype : 'checkbox'
		},{
			name : 'isNewFun',
			fieldLabel : system.MenuItem.isNewFunction,
			xtype : 'checkbox'
		}],
		buttons: [{
			text: i18n.global.save,
			handler: function(){ save(formMenuItem, 'MenuItem');}
		}, {
			text: i18n.global.del,
			handler: function(){
				remove(formMenuItem, AuthFunctionService.saveOrUpdateMenuItem);
			}
		}]
	});
	
	var formViewButton = new Ext.form.FormPanel({
		iconCls : 'viewButton',
		title: system.ViewButton.ViewButton,
		disabled: true,
		labelWidth:60,
		buttonAlign: 'left',
		defaults: {width: 300},
		defaultType: 'textfield',
		items:[{
			name: 'buttonUrl',
			fieldLabel: system.ViewButton.buttonUrl
		}, {
			name: 'btnName',
			allowBlank: false,
			fieldLabel: system.ViewButton.btnName
		}, {
			name: 'btnTitleKey',
			allowBlank: false,
			fieldLabel: system.ViewButton.btnTitleKey
		}, {
			name: 'btnMsgKey',
			allowBlank: false,
			fieldLabel: system.ViewButton.btnMsgKey
		}],
		buttons: [{
			text: i18n.global.save,
			handler: function(){ save(formViewButton, 'ViewButton');}
		}, {
			text: i18n.global.del,
			handler: function(){
				remove(formViewButton, AuthFunctionService.saveOrUpdateViewButton);
			}
		}]
	});
	
	var authTab = new Ext.TabPanel({
		activeTab: 0,
		deferredRender: false,
		defaults: {autoHeight: true, bodyStyle:'padding:10px'},
		items: [formModule, formMenuGroup, formMenuItem, formViewButton]
	});
	
	var btnNewMD = new Ext.Button({
		iconCls : 'module',
		text: i18n.global.add + ' ' + system.Module.Module,
		disabled: false,
		handler: function(){
			changeState('Module');
			pnlMain.topToolbar.disable();
			formModule.editingRec = new recModule({
				rowStatus : Gfa.GlobalConstant.ROWSTATUS_ADDED
			});
		}
	});
	var btnNewMG = new Ext.Button({
		iconCls : 'menuGroup',
		text: i18n.global.add + ' ' + system.MenuGroup.MenuGroup,
		disabled: true,
		handler: function(){
			changeState('MenuGroup');
			pnlMain.topToolbar.disable();
			var parentNode = getParentNode('MenuGroup');
			formMenuGroup.editingRec = new recMenuGroup({
				module : parentNode.authData,
				rowStatus : Gfa.GlobalConstant.ROWSTATUS_ADDED
			});
		}
	});
	var btnNewMI = new Ext.Button({
		iconCls : 'menuItem',
		text: i18n.global.add + ' ' + system.MenuItem.MenuItem,
		disabled: true,
		handler: function(){
			changeState('MenuItem');
			pnlMain.topToolbar.disable();
			var parentNode = getParentNode('MenuItem');
			formMenuItem.editingRec = new recMenuItem({
				menuGroup : parentNode.authData,
				rowStatus : Gfa.GlobalConstant.ROWSTATUS_ADDED
			});
		}
	});
	var btnNewVB = new Ext.Button({
		iconCls : 'viewButton',
		text: i18n.global.add + ' ' + system.ViewButton.ViewButton,
		disabled: true,
		handler: function(){
			changeState('ViewButton');
			pnlMain.topToolbar.disable();
			var parentNode = getParentNode('ViewButton');
			formViewButton.editingRec = new recViewButton({
				menuItem : parentNode.authData,
				rowStatus : Gfa.GlobalConstant.ROWSTATUS_ADDED
			});
		}
	});
	
	var pnlMain = new Ext.Panel({
		region: 'center',
		margins : '5 5 5 0',
		border: true,
		tbar: [btnNewMD, btnNewMG, btnNewMI, btnNewVB],
		items: authTab
	});
		
	var pnlFnMgr = new Ext.Panel({
		layout: 'border',
		items: [authTree, pnlMain]
	});
	
	//get current tab
	var cTab = Ext.getCmp(Gfa.GlobalVariable.contentAreaId).activeTab;
	cTab.add(pnlFnMgr);
	cTab.doLayout(); //render 'fit'
	
	function save(formPanel, authType) {
		if (!formPanel.form.isValid() 
				&& !isDirty(authType)) {
			return;
		}
		
		var record = formPanel.editingRec;
		
		var result = createChildren(record, authType);
		
		if(result == 'langIsDuplicate'){
			alert('语言不能重复！');
			return;
		}
		
		if(result == 'titleIsNull'){
			alert('标题不能为空！');
			return;
		}
		
		if(result == 'notEnough'){
			alert('每种语言必须有且仅有一条记录！');
			return;
		}
		
		formPanel.form.updateRecord(record);
		
		if (record.dirty || isDirty(authType)) {
			if (record.data.rowStatus != Gfa.GlobalConstant.ROWSTATUS_ADDED) {
				record.data.rowStatus = Gfa.GlobalConstant.ROWSTATUS_MODIFIED;
			}
			
			var _text, _iconCls;
			var mask = new Ext.LoadMask(formPanel.el,{msg : i18n.message.saveing});
			var callback = {
				callback : function (result){
					if (record.data.rowStatus == Gfa.GlobalConstant.ROWSTATUS_ADDED) {
						var parentNode = getParentNode(authType);
						var newNode = new Ext.tree.TreeNode({
							text : Gfa.CommonUtil.matchLanguage(result[_text]),
							iconCls : _iconCls
						});
				    	newNode.authType = authType;
				    	newNode.authData = result;
				    	parentNode.appendChild(newNode);
				    	authTree.fireEvent('click', newNode);
					} else if (record.data.rowStatus == Gfa.GlobalConstant.ROWSTATUS_MODIFIED) {
						var selNode = authTree.selModel.selNode;
						selNode.setText(Gfa.CommonUtil.matchLanguage(result[_text]));
						selNode.authData = result;
					}
					record.data = result;
					record.commit();
					
					if(authType == 'Module'){
						dsModuleTitle.load();
					}
					if(authType == 'MenuGroup'){
						dsMenuGroupTitle.load();
					}
					if(authType == 'MenuItem'){
						dsMenuItemTitle.load();
					}
				},
				preHook : function() {mask.show();},
				postHook : function() {mask.hide();mask.destroy();}
			};
			
			switch (authType) {
				case 'Module':
					_text = 'moduleTitleKey'; _iconCls = 'module';
					AuthFunctionService.saveOrUpdateModule(record.data, callback);
					break;
				case 'MenuGroup':
					_text = 'menuGrpTitleKey'; _iconCls = 'menuGroup';
					AuthFunctionService.saveOrUpdateMenuGroup(record.data, callback);
					break;
				case 'MenuItem':
					_text = 'menuItemTitleKey'; _iconCls = 'menuItem';
					AuthFunctionService.saveOrUpdateMenuItem(record.data, callback);
					break;
				case 'ViewButton':
					_text = 'btnTitleKey'; _iconCls = 'viewButton';
					AuthFunctionService.saveOrUpdateViewButton(record.data, callback);
					break;
			}
		}
	}
	
	function isDuplicate(astr){  
		return /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f" + astr.join("\x0f\x0f") +"\x0f");  
	} 
	
	function createChildren(record, authType){
		switch (authType) {
			case 'Module':
				var moduleTitles = [];
				var langs = [];
				var titleIsNull = false;
				Ext.each(gridModuleTitle.store.data.items, function(item){
					if(!item.data.title || item.data.title == ''){
						titleIsNull = true;
					}
					langs.push(item.data.lang);
					moduleTitles.push(item.data);
				});
				record.data.moduleTitles = moduleTitles;
				if(titleIsNull){
					return 'titleIsNull';
				}
				if(isDuplicate(langs)){
					return "langIsDuplicate";
				}
				if(langs.length != 3){
					return "notEnough";
				}
				break;
			case 'MenuGroup':
				var menuGroupTitles = [];
				var langs = [];
				var titleIsNull = false;
				Ext.each(gridMenuGroupTitle.store.data.items, function(item){
					if(!item.data.title || item.data.title == ''){
						titleIsNull = true;
					}
					langs.push(item.data.lang);
					menuGroupTitles.push(item.data);
				});
				record.data.menuGroupTitles = menuGroupTitles;
				if(titleIsNull){
					return 'titleIsNull';
				}
				if(isDuplicate(langs)){
					return "langIsDuplicate";
				}
				if(langs.length != 3){
					return "notEnough";
				}
				break;
			case 'MenuItem':
				var menuItemTitles = [];
				var langs = [];
				var titleIsNull = false;
				Ext.each(gridMenuItemTitle.store.data.items, function(item){
					if(!item.data.title || item.data.title == ''){
						titleIsNull = true;
					}
					langs.push(item.data.lang);
					menuItemTitles.push(item.data);
				});
				record.data.menuItemTitles = menuItemTitles;
				if(titleIsNull){
					return 'titleIsNull';
				}
				if(isDuplicate(langs)){
					return "langIsDuplicate";
				}
				if(langs.length != 3){
					return "notEnough";
				}
				break;
			case 'ViewButton':
				break;
			default :
				break;
		}
	}
	
	function isDirty(authType){
		switch (authType) {
			case 'Module':
				return gridModuleTitle.store.modified && gridModuleTitle.store.modified.length >0;
				break;
			case 'MenuGroup':
				return gridMenuGroupTitle.store.modified && gridMenuGroupTitle.store.modified.length >0;
				break;
			case 'MenuItem':
				return gridMenuItemTitle.store.modified && gridMenuItemTitle.store.modified.length >0;
				break;
			case 'ViewButton':
				return false;
				break;
			default :
				return false;
				break;
		}
	}
	
	function remove(formPanel, saveHandler) {
		Ext.Msg.confirm(i18n.message.prompt, i18n.message.confirmDel, function(btn){
			if (btn == 'yes') {
				var data = formPanel.editingRec.data;
				formPanel.form.reset();
				formPanel.disable();
				pnlMain.topToolbar.disable();
				btnNewMD.enable();
				if (data.rowStatus != Gfa.GlobalConstant.ROWSTATUS_ADDED) {
					data.rowStatus = Gfa.GlobalConstant.ROWSTATUS_DELETED;
					saveHandler(data, function(result){
						var currentNode = authTree.selModel.selNode;
						currentNode.remove();
					});
				}
				gridMenuItemTitle.getStore().removeAll();
			}
		});
	}
	
	function getParentNode(authType) {
		var node = authTree.selModel.selNode;
		switch(authType) {
			case 'Module':
				return root;
			case 'MenuGroup':
				if (node.authType == 'Module') {
					return node;
				} else if (node.authType == 'MenuGroup') {
					return node.parentNode;
				} else if (node.authType == 'MenuItem') {
					return node.parentNode.parentNode;
				} else if (node.authType == 'ViewButton') {
					return node.parentNode.parentNode.parentNode;
				}
			case 'MenuItem':
				if (node.authType == 'MenuGroup') {
					return node;
				} else if (node.authType == 'MenuItem') {
					return node.parentNode;
				} else if (node.authType == 'ViewButton') {
					return node.parentNode.parentNode;
				}
			case 'ViewButton':
				if (node.authType == 'MenuItem') {
					return node;
				} else if (node.authType == 'ViewButton') {
					return node.parentNode;
				}
		}
	}
	
	function changeState(authType) {
		var formPanel;
		authTab.activeTab.disable();
		pnlMain.topToolbar.enable();
		switch(authType) {
			case 'Module':
				btnNewMI.disable();
				btnNewVB.disable();
				gridModuleTitle.getStore().removeAll();
				formPanel = formModule;
				break;
			case 'MenuGroup':
				btnNewVB.disable();
				gridMenuGroupTitle.getStore().removeAll();
				formPanel = formMenuGroup;
				break;
			case 'MenuItem':
				gridMenuItemTitle.getStore().removeAll();
				formPanel = formMenuItem;
				break;
			case 'ViewButton':
				formPanel = formViewButton;
				break;
		}
		formPanel.enable();
		formPanel.form.reset();
		formPanel.show();
	}
});
